#include "serialportdialog.h"
#include "ui_serialportdialog.h"

SerialPortDialog::SerialPortDialog(SerialPortData *pdata, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::serialPortDialog)
{
    ui->setupUi(this);
    m_pData = pdata;

    initGui();
}

SerialPortDialog::~SerialPortDialog()
{
    delete ui;
}

void SerialPortDialog::accept()
{
    QString name = ui->serialPortComboBox->currentText();
    if (name.length()) {
        m_pData->name = ui->serialPortComboBox->currentText();
        m_pData->baudRate = ui->baudRateComboBox->itemData(ui->baudRateComboBox->currentIndex()).toInt();
        m_pData->dataBits = ui->dataBitsComboBox->itemData(ui->dataBitsComboBox->currentIndex()).toInt();
        m_pData->parity = ui->parityComboBox->currentIndex();
        m_pData->stopBits = ui->stopBitsComboBox->currentIndex();
        m_pData->flowControl = ui->flowControlComboBox->currentIndex();

        QDialog::accept();
    }
}

void SerialPortDialog::initGui()
{
    static const QString blankString = QObject::tr("N/A");
    QString description, manufacturer;
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
        QStringList list;
        description = info.description();
        manufacturer = info.manufacturer();
        list << info.portName()
             << (!description.isEmpty() ? description : blankString)
             << (!manufacturer.isEmpty() ? manufacturer : blankString)
             << info.systemLocation()
             << (info.vendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : blankString)
             << (info.productIdentifier() ? QString::number(info.productIdentifier(), 16) : blankString);
        qDebug() << list;

        ui->serialPortComboBox->addItem(list.first(), list);
    }
    ui->serialPortComboBox->setCurrentIndex(ui->serialPortComboBox->findText(m_pData->name));

    ui->baudRateComboBox->addItem(QStringLiteral("1200"), QSerialPort::Baud1200);
    ui->baudRateComboBox->addItem(QStringLiteral("2400"), QSerialPort::Baud2400);
    ui->baudRateComboBox->addItem(QStringLiteral("4800"), QSerialPort::Baud4800);
    ui->baudRateComboBox->addItem(QStringLiteral("9600"), QSerialPort::Baud9600);
    ui->baudRateComboBox->addItem(QStringLiteral("19200"), QSerialPort::Baud19200);
    ui->baudRateComboBox->addItem(QStringLiteral("38400"), QSerialPort::Baud38400);
    ui->baudRateComboBox->addItem(QStringLiteral("115200"), QSerialPort::Baud115200);
    ui->baudRateComboBox->setCurrentIndex(ui->baudRateComboBox->findText(QString::number(m_pData->baudRate)));

    ui->dataBitsComboBox->addItem(QStringLiteral("5"), QSerialPort::Data5);
    ui->dataBitsComboBox->addItem(QStringLiteral("6"), QSerialPort::Data6);
    ui->dataBitsComboBox->addItem(QStringLiteral("7"), QSerialPort::Data7);
    ui->dataBitsComboBox->addItem(QStringLiteral("8"), QSerialPort::Data8);
    ui->dataBitsComboBox->setCurrentIndex(ui->dataBitsComboBox->findText(QString::number(m_pData->dataBits)));

    ui->parityComboBox->addItem(QStringLiteral("None"), QSerialPort::NoParity);
    ui->parityComboBox->addItem(QStringLiteral("Even"), QSerialPort::EvenParity);
    ui->parityComboBox->addItem(QStringLiteral("Odd"), QSerialPort::OddParity);
    ui->parityComboBox->addItem(QStringLiteral("Mark"), QSerialPort::MarkParity);
    ui->parityComboBox->addItem(QStringLiteral("Space"), QSerialPort::SpaceParity);
    ui->parityComboBox->setCurrentIndex(m_pData->parity);

    ui->stopBitsComboBox->addItem(QStringLiteral("1"), QSerialPort::OneStop);
    ui->stopBitsComboBox->addItem(QStringLiteral("1.5"), QSerialPort::OneAndHalfStop);
    ui->stopBitsComboBox->addItem(QStringLiteral("2"), QSerialPort::TwoStop);
    ui->stopBitsComboBox->setCurrentIndex(m_pData->stopBits);

    ui->flowControlComboBox->addItem(QStringLiteral("None"), QSerialPort::NoFlowControl);
    ui->flowControlComboBox->addItem(QStringLiteral("RTS/CTS"), QSerialPort::HardwareControl);
    ui->flowControlComboBox->addItem(QStringLiteral("XON/XOFF"), QSerialPort::SoftwareControl);
    ui->flowControlComboBox->setCurrentIndex(m_pData->flowControl);
}
